package cn.geoportal.benoserver.controller;


import cn.geoportal.benoserver.entity.common.RespBean;
import cn.geoportal.benoserver.entity.vo.DictionaryDes;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: gmo
 * @Package: cn.geoportal.benoserver.controller
 * @Author: wangminyang
 * @Description: 数据目录controller
 */
@Api(tags = "数据目录")
@ApiSupport(order = 401)    // 分组排序
@RestController
@PropertySource("/application.yml")
@RequestMapping("/api/dataDictionary")
public class DataDictionaryController {

    @Value("${originDataDirectory}")
    private String originDataDirectory;

    @Value("${resultDataDirectory}")
    private String resultDataDirectory;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取文件夹及文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name="type", value = "目录类型（origin：原始数据目录，result：成果数据目录）", required = true, example = "origin"),
            @ApiImplicitParam(name="path", value = "路径", required = false, example = "\\aa\\bb"),
            @ApiImplicitParam(name="onlyDIct", value = "仅文件夹", required = false, example = "false"),
            @ApiImplicitParam(name="fileType", value = "文件类型", required = false, example = ".shp,.shx")
    })
    @RequestMapping(value="/get", method = RequestMethod.GET)
    public RespBean list(@RequestParam(value = "type",defaultValue = "all") String type,
                         @RequestParam(value = "path",defaultValue = "\\") String path,
                         @RequestParam(value = "fileType",required = false) String fileType,
                         @RequestParam(value = "onlyDict",required = false, defaultValue="false") boolean onlyDict){
        List<DictionaryDes> list = new ArrayList<>();
        try{
            String filepath = "";
            if("\\".equals(path)){
                if("origin".equals(type)){//原始数据目录
                    filepath = originDataDirectory + path;
                }else  if("result".equals(type)){//成果数据目录
                    filepath = resultDataDirectory + path;
                }
            }else {
                filepath = path;
            }
            File file = new File(filepath);//File类型可以是文件也可以是文件夹
            File[] fileList = file.listFiles();//将该目录下的所有文件放置在一个File类型的数组中
            String[] split = null;
            if(StringUtils.isNotEmpty((fileType))){
                split = fileType.split(",");
            }
            for(File f : fileList){
                String fileName = f.getName();
                boolean filter = true;
                if(!f.isDirectory() && split!=null){
                    for(String suffic : split){
                        if(fileName.endsWith(suffic)){
                            filter = false;
                        }
                    }

                    if(filter){
                        continue;
                    }
                }

                DictionaryDes dicDes = new DictionaryDes();
                dicDes.setName(fileName);
                if(f.isDirectory()){
                    dicDes.setType(0);
                }else{
                    dicDes.setType(1);
                    if(onlyDict){
                        continue;
                    }
                }
                if(!filepath.endsWith("\\")){
                    filepath = filepath +"\\";
                }

                dicDes.setPath(filepath+fileName);
                list.add(dicDes);
            }
            return RespBean.success("获取成功", list);
        }catch(Exception e){
            e.printStackTrace();
            return RespBean.error("服务器异常");
        }
    }
}
